import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor(protected configService: ConfigService) {
        super({
        jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
        ignoreExpiration: false,
        secretOrKey: process.env.SECRET,
        });
    }
    async validate(payload: any) {
        // 这里校验token时  payload的字段取决于生成token的配置
        // return的返回结果 在 canActivate 方法的context上获取到  从而去进行 权限管理
        return { userId: payload.userid, username: payload.username };
    }
}
